package org.jetnuke.blocks;

import org.apache.log4j.Logger;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IBlock;
import org.jetnuke.common.IBlockInfo;
import org.jetnuke.common.IUserInfo;

public class RssBlock implements IBlock {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RssBlock.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IBlock#serviceBlock(javax.servlet.http.HttpServletRequest,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IBlockInfo,
	 *      org.jetnuke.common.IUserInfo, org.jetnuke.common.IAdminInfo)
	 */
	public void serviceBlock(HttpServletRequest req, VelocityContext vc,
			IBlockInfo blockInfo, IUserInfo userInfo, IAdminInfo adminInfo)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("serviceBlock(HttpServletRequest, VelocityContext, IBlockInfo, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		vc.put("_TITLE", blockInfo.getTitle());
		String url = blockInfo.getRssUrl();
		if (url == null) {
			if (logger.isDebugEnabled()) {
				logger
						.debug("serviceBlock(HttpServletRequest, VelocityContext, IBlockInfo, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
			}
			return;
		}
		if (!url.startsWith("http://")) {
			if (!url.startsWith("https://")) {
				url = "http://" + url;
			}
		}
		url = URLEncoder.encode(url, "UTF-8");
		vc.put("url", url);

		if (logger.isDebugEnabled()) {
			logger
					.debug("serviceBlock(HttpServletRequest, VelocityContext, IBlockInfo, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
	}

}
